<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_tipe_jurnal_coa extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_tipe_jurnal_model', 'TipeJurnal');
		$this->load->model('keu_tipe_jurnal_coa_model', 'TipeJurnalCOA');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_coa_model', 'Coa');
		
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = $this->session_menu_label;
		$data['data'] = null;
		
		
		viewByTemplate('keu_tipe_jurnal_coa/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->TipeJurnal->GetJSON();
	}
	
	function dtl_jurnal_data()
	{
		$this->secure_session();
		echo $this->TipeJurnalCOA->GetJSON();
	}
	
	
	
		
	function create()
	{
		$data['title'] = "Tambah ".$this->session_menu_label;
		$data['data'] = NULL;
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		
		$data['mode'] = "create";
		viewByTemplate('keu_tipe_jurnal_coa/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Ubah ".$this->session_menu_label;
		$data['data'] = $this->TipeJurnal->GetSingleView(get('kd_tipe_jurnal'));
				
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']		= json_encode($this->Combo->SWCostCenter());
		
		
		$data['mode'] = "edit";
		viewByTemplate('keu_tipe_jurnal_coa/form', $data, 'form');
	}

	
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		
		return $error;
	}
	
	function dtl_account_data()
	{
		echo $this->TipeJurnalCOA->GetJSON();
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_tipe_jurnal'));
				
				// Detail Payment
				$dtl_account	= $this->data['dtl_account'];
				$total_deleted	= $dtl_account['num_row']['deleted'];
				$total_inserted	= $dtl_account['num_row']['inserted'];
				$total_updated	= $dtl_account['num_row']['updated'];
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_account['deleted'][$i];
					$this->TipeJurnalCOA->DeleteSingle($data['kd_tipe_jurnal'], $row['id_coa']);
				}
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_account['inserted'][$i];
					
					unset($row['nama_coa']);
					unset($row['nama_cost_center']);
					
					$row['no_urut']			= toNumber($row['no_urut'])=='NaN' ? 0 : toNumber($row['no_urut']);
					$row['kd_tipe_jurnal']  = $data['kd_tipe_jurnal'];				
					
					$row['create_date']	= NOW;
					$row['create_user']	= USERID;
					
					$this->TipeJurnalCOA->Add($row);
				}
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_account['updated'][$i];
				
					unset($row['nama_coa']);
					unset($row['nama_cost_center']);
					
					$row['no_urut']		= toNumber($row['no_urut'])=='NaN' ? 0 : toNumber($row['no_urut']);
					
					$row['modify_date']	= NOW;
					$row['modify_user']	= USERID;
				
				
					$this->TipeJurnalCOA->Update($row, array(	'kd_tipe_jurnal'	=> $data['kd_tipe_jurnal'],
																'id_coa'		=>  $row['id_coa'] ));
				}
				
				$tpl_update_success = $this->MessageTemplate("CS002");
				$ls_response = $tpl_update_success['message_id'];
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}


	
}
?>